
from Case.python_ui.Common.basepage import Basepage

import time,pytest

from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.PageLocate.admin_team_ticket_loactor import AdminTeamTicketLocator
from Case.python_ui.PageLocate.admin_ticket_management_locator import AdminTicketManagementLocator
from Case.python_ui.PageLocate.admin_pu_role_locator import AdminPURoleLocator
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.PageLocate.role_locator import Rolelocator


class AdminTeamTicketPage(Basepage):
    # 进入team ticket菜单页面
    def enter_team_ticket_page(self):
        time.sleep(2)
        doc = "进入team ticket菜单"
        for i in range(3):
            try:
                self.wait_elevisble(AdminTicketManagementLocator.dashboard_tab, doc)
                self.click_element(AdminTicketManagementLocator.dashboard_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminTicketManagementLocator.bill_tab, doc)
                self.click_element(AdminTicketManagementLocator.bill_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminTeamTicketLocator.team_ticket_tab, doc)
                self.click_element(AdminTeamTicketLocator.team_ticket_tab, doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    def loop_operation(self,locate,type,times):
        if type == "Wait":
            doc = "循环等待元素显示"
            for i in range(times):
                try:
                    self.wait_elevisble(locate, doc)
                    break
                except:
                    time.sleep(10)
                    continue
        if type == "Click":
            doc = "循环等待点击操作执行成功"
            for i in range(times):
                try:
                    self.click_element(locate,doc)
                    break
                except:
                    time.sleep(10)
                    continue


    #开启工单管理员在线开关
    def ticket_admin_online(self):
        time.sleep(2)
        doc = "开启工单管理员在线开关"
        self.wait_elevisble(AdminTeamTicketLocator.admin_avatar,doc)
        self.click_element(AdminTeamTicketLocator.admin_avatar,doc)
        self.wait_elevisble(AdminTeamTicketLocator.ticket_admin_online_status,doc)
        status = self.get_element(AdminTeamTicketLocator.ticket_admin_online_status,doc).text
        if(status == "Online"):
            assert "1" == "1"
        else:
            self.wait_elevisble(AdminTeamTicketLocator.ticket_admin_online_switch,doc)
            self.click_element(AdminTeamTicketLocator.ticket_admin_online_switch,doc)

            self.wait_elevisble(AdminTeamTicketLocator.ticket_admin_online_status,doc)
            status = self.get_element(AdminTeamTicketLocator.ticket_admin_online_status,doc).text
            assert status == "Online"

    #assign工单给管理员--不指定id
    def test_assign_ticket(self):
        doc = "assign工单给管理员"
        self.enter_team_ticket_page()
        time.sleep(2)
        #筛选pending状态的工单
        self.wait_elevisble(AdminTeamTicketLocator.ticket_status_filter,doc)
        self.click_element(AdminTeamTicketLocator.ticket_status_filter,doc)
        self.wait_elevisble(AdminTeamTicketLocator.reset,doc)
        self.click_element(AdminTeamTicketLocator.reset,doc)
        self.wait_elevisble(AdminTeamTicketLocator.pending_for_service,doc)
        self.click_element(AdminTeamTicketLocator.pending_for_service,doc)
        self.wait_elevisble(AdminTeamTicketLocator.ok,doc)
        self.click_element(AdminTeamTicketLocator.ok,doc)
        time.sleep(2)
        self.wait_elevisble(AdminTeamTicketLocator.search_ticket_last_id, doc)
        ticket_id = self.get_element(AdminTeamTicketLocator.search_ticket_last_id,doc).text
        self.wait_elevisble(AdminTeamTicketLocator.team_ticket_assign_button,doc)
        self.click_element(AdminTeamTicketLocator.team_ticket_assign_button,doc)
        self.click_element(AdminTeamTicketLocator.support_member_list,doc)
        self.wait_elevisble(AdminTeamTicketLocator.support_member_selector,doc)
        self.click_element(AdminTeamTicketLocator.support_member_selector,doc)
        self.wait_elevisble(AdminTeamTicketLocator.assign_remark,doc)
        self.send_keys(AdminTeamTicketLocator.assign_remark,"assign to admin",doc)
        self.wait_elevisble(AdminTeamTicketLocator.assign_confirm_buttton,doc)
        self.click_element(AdminTeamTicketLocator.assign_confirm_buttton,doc)
        time.sleep(2)
        status = self.search_ticket_by_id(ticket_id)
        assert status == 'Solving'

    # assign工单给管理员--指定id
    def assign_ticket_use_id(self,ticket_id):
        doc = "指定ticket_id，assign工单给管理员"
        # 筛选pending状态的工单
        self.search_ticket_by_id(ticket_id)
        self.wait_elevisble(AdminTeamTicketLocator.search_ticket_last_id, doc)
        ticket_id = self.get_element(AdminTeamTicketLocator.search_ticket_last_id, doc).text
        self.wait_elevisble(AdminTeamTicketLocator.team_ticket_assign_button, doc)
        self.click_element(AdminTeamTicketLocator.team_ticket_assign_button, doc)
        self.click_element(AdminTeamTicketLocator.support_member_list, doc)
        self.wait_elevisble(AdminTeamTicketLocator.support_member_selector, doc)
        self.click_element(AdminTeamTicketLocator.support_member_selector, doc)
        self.wait_elevisble(AdminTeamTicketLocator.assign_remark, doc)
        self.send_keys(AdminTeamTicketLocator.assign_remark, "assign to admin", doc)
        self.wait_elevisble(AdminTeamTicketLocator.assign_confirm_buttton, doc)
        self.click_element(AdminTeamTicketLocator.assign_confirm_buttton, doc)
        time.sleep(2)
        status = self.search_ticket_by_id(ticket_id)
        assert status == 'Solving'

    #搜索工单
    def search_ticket_by_id(self,id):
        doc = "搜索工单"
        self.wait_elevisble(AdminTeamTicketLocator.ticket_status_filter,doc)
        # for i in range(6):
        #     try:
        #         self.click_element(AdminTeamTicketLocator.ticket_status_filter,doc)
        #         break
        #     except:
        #         time.sleep(10)
        #         continue
        self.loop_operation(AdminTeamTicketLocator.ticket_status_filter,"Click",6)
        self.wait_elevisble(AdminTeamTicketLocator.reset,doc)
        self.click_element(AdminTeamTicketLocator.reset,doc)
        self.wait_elevisble(AdminTeamTicketLocator.ok,doc)
        self.click_element(AdminTeamTicketLocator.ok,doc)
        self.wait_elevisble(AdminTeamTicketLocator.search_box,doc)
        self.keyboard_event(AdminTeamTicketLocator.search_box,doc)
        self.send_keys(AdminTeamTicketLocator.search_box,id,doc)
        self.wait_elevisble(AdminTeamTicketLocator.search_button,doc)
        self.click_element(AdminTeamTicketLocator.search_button,doc)
        time.sleep(3)
        self.wait_elevisble(AdminTeamTicketLocator.search_ticket_last_id,doc)
        ticket_id = self.get_element(AdminTeamTicketLocator.search_ticket_last_id,doc).text
        assert id == ticket_id
        self.wait_elevisble(AdminTeamTicketLocator.search_ticket_status, doc)
        status = self.get_element(AdminTeamTicketLocator.search_ticket_status, doc).text
        return status

    #进入工单详情页
    def enter_ticket_detail_page(self):
        doc = "进入工单详情页"
        self.enter_team_ticket_page()
        time.sleep(2)
        self.wait_elevisble(AdminTeamTicketLocator.team_ticket_last_id,doc)
        ticket_no_1 = self.get_element(AdminTeamTicketLocator.team_ticket_last_id,doc).text
        self.click_element(AdminTeamTicketLocator.team_ticket_last_id,doc)
        return ticket_no_1

    def enter_ticket_detail_page_after_assign_ticket(self):
        doc = "先分配工单，再进入工单详情页"
        self.wait_elevisble(AdminTeamTicketLocator.team_ticket_last_id,doc)
        ticket_no_1 = self.get_element(AdminTeamTicketLocator.team_ticket_last_id,doc).text
        self.click_element(AdminTeamTicketLocator.team_ticket_last_id,doc)
        return ticket_no_1

    def get_ticket_id_in_detai_page(self):
        doc = "在工单详情页面，获取ticket id"
        self.wait_elevisble(AdminTeamTicketLocator.ticket_id_in_detail_page,doc)
        ticket_no_2 = self.get_element(AdminTeamTicketLocator.ticket_id_in_detail_page,doc).text
        return ticket_no_2

    def admin_add_comment(self):
        doc = "管理员发送工单消息"

    def admin_check_pending_message(self,ticket_id):
        self.enter_team_ticket_page()
        self.search_ticket_by_id(ticket_id)
        doc = "管理员检查pending提醒"
        self.wait_elevisble(AdminTeamTicketLocator.tickets_button2,doc)
        self.click_element(AdminTeamTicketLocator.tickets_button2,doc)
        self.wait_elevisble(AdminTeamTicketLocator.team_ticket_pending,doc)
        self.wait_elevisble(AdminTeamTicketLocator.team_ticket_menu_pending,doc)
        pending = self.get_element(AdminTeamTicketLocator.team_ticket_menu_pending,doc).text
        assert pending == 'PENDING'


    def admin_modify_ticket_priority(self):
        doc = "修改工单优先级"
        self.wait_elevisble(AdminTeamTicketLocator.modify_button,doc)
        time.sleep(2)
        self.click_element(AdminTeamTicketLocator.modify_button,doc)
        self.wait_elevisble(AdminTeamTicketLocator.priority_level,doc)
        self.click_element(AdminTeamTicketLocator.priority_level,doc)
        self.wait_elevisble(AdminTeamTicketLocator.priority_level_high,doc)
        self.click_element(AdminTeamTicketLocator.priority_level_high,doc)
        self.wait_elevisble(AdminTeamTicketLocator.modify_button_confirm,doc)
        self.click_element(AdminTeamTicketLocator.modify_button_confirm,doc)


    def admin_check_modify_result(self):
        doc = "校验工单优先级是否改成功"
        self.wait_elevisble(AdminTeamTicketLocator.search_button,doc)
        self.click_element(AdminTeamTicketLocator.search_button,doc)
        time.sleep(2)
        self.wait_elevisble(AdminTeamTicketLocator.search_ticket_priority,doc)
        status = self.get_element(AdminTeamTicketLocator.search_ticket_priority,doc).text
        print(">>>>>>>>>>>>status:",status)
        assert status == 'High'

    def check_auth(self,name):
        auth = "Assign Solve Transfer View"
        if name in auth:
            assert 1 == 1
        else:
            print(">>>>>>>>>>>权限校验不通过，当前权限：",name)

    def enter_role_premission_page(self):
        doc = "进入role的权限编辑页面"
        time.sleep(3)
        self.wait_elevisble(AdminPURoleLocator.create_role_button, doc)
        self.click_element(AdminPURoleLocator.create_role_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminPURoleLocator.create_role_name, doc)
        role_name = CommonTool.generate_string()
        self.send_keys(AdminPURoleLocator.create_role_name, role_name, doc)
        time.sleep(2)
        self.click_element(AdminPURoleLocator.creat_next_button, doc)
        self.wait_elevisble(AdminPURoleLocator.basic_information_button, doc)

    def check_ticket_auth(self):
        doc = "检查工单的按钮权限"
        #进入创建role页面，一直到权限树的页面
        self.enter_role_premission_page()
        i = 10
        while i < 14:
            self.wait_elevisble(AdminTeamTicketLocator.get_auth_name(self,i),doc)
            name = self.get_element(AdminTeamTicketLocator.get_auth_name(self,i),doc).text
            if name == "Ticket":
                self.wait_elevisble(AdminTeamTicketLocator.get_expand_button(self,i-1),doc)
                self.click_element(AdminTeamTicketLocator.get_expand_button(self,i-1),doc)
                time.sleep(2)
                self.wait_elevisble(AdminTeamTicketLocator.team_ticket_auth_edit,doc)
                self.click_element(AdminTeamTicketLocator.team_ticket_auth_edit,doc)
                time.sleep(2)
                self.wait_elevisble(AdminTeamTicketLocator.assign_auth,doc)
                auth_name = self.get_element(AdminTeamTicketLocator.assign_auth,doc).text
                self.check_auth(auth_name)
                self.wait_elevisble(AdminTeamTicketLocator.solve_auth,doc)
                auth_name = self.get_element(AdminTeamTicketLocator.solve_auth, doc).text
                self.check_auth(auth_name)
                self.wait_elevisble(AdminTeamTicketLocator.transfer_auth, doc)
                auth_name = self.get_element(AdminTeamTicketLocator.transfer_auth, doc).text
                self.check_auth(auth_name)
                self.wait_elevisble(AdminTeamTicketLocator.view_auth, doc)
                auth_name = self.get_element(AdminTeamTicketLocator.view_auth, doc).text
                self.check_auth(auth_name)
                break
            i = i + 1



    def check_ticket_tag(self,ticket_id,tag):
        doc = "检查工单标签"
        self.wait_elevisble(AdminTeamTicketLocator.team_ticket_tab, doc)
        self.click_element(AdminTeamTicketLocator.team_ticket_tab, doc)
        time.sleep(3)
        self.search_ticket_by_id(ticket_id)
        self.wait_elevisble(AdminTeamTicketLocator.ticket_tag,doc)
        value = self.get_element(AdminTeamTicketLocator.ticket_tag,doc).text
        assert tag == value

